package com.yulinlin.admin.modules.generate.controller;

import cn.hutool.core.util.ZipUtil;
import com.yulinlin.admin.modules.generate.domain.ViewEnum;
import com.yulinlin.admin.modules.generate.domain.config.GenerateConfig;
import com.yulinlin.admin.modules.generate.domain.po.GeneratePo;
import com.yulinlin.admin.modules.generate.service.GenerateService;
import com.yulinlin.common.domain.ResponseVo;
import com.yulinlin.common.util.ResponseUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@Api(tags = "生成器")
@RestController
@RequestMapping("generate")
public class GenerateController {


    @Autowired
    private GenerateService service;


    @RequestMapping(value = "config",method = RequestMethod.POST)
    public ResponseVo config(){
        GenerateConfig config =  service.getGenerateConfig();
        return ResponseVo.newInstance(config);
    }



    @RequestMapping(value = "generateCode",method = RequestMethod.POST)
    public void generateCode( GeneratePo po) throws Exception{
        GeneratePo data =  po;
        String folder = System.currentTimeMillis()+"";
        String path = "D:/upload/zip/"+folder;
        data.setWritePath(path);
        //data.setPackageSpace("com.yulinlin.admin");

        for (ViewEnum viewEnum : po.getViews()) {
            service.generateCode(data,viewEnum);
        }


        File f =  ZipUtil.zip(path);

        ResponseUtil.downloadFile(f);

        f.delete();



    }
}
